<?php
namespace app\api\controller;

use app\lib\exception\ParamException;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
class Index
{

    public function index()
    {
        $options = new ChromeOptions();
        $options->addArguments(['--no-sandbox','--headless']);

        $capabilities = DesiredCapabilities::chrome();
        $capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

        $host = 'http://localhost:4444';
        $driver = RemoteWebDriver::create($host, $capabilities);

        $pages = \db('page')->limit(10)->order('id desc')->column('online_url');
        foreach ($pages as $v) {
            $url = $v;
            // 访问网站
            $driver->get($url);

            $performance = $driver->executeScript('return window.performance.timing');
            $pageloadtime = $performance['loadEventEnd'] - $performance['navigationStart'];

            echo $v." Load Time ". $pageloadtime. 'ms'. "\r\n";
        }

        // 关闭 WebDriver 会话
        $driver->quit();
        echo "非法请求！！！";
    }
}